package org.spark.api;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.spark.api.config.HBaseInstance;
import org.spark.api.config.HBaseTable;
import org.spark.api.util.DateUtils;
import java.io.IOException;
import java.util.List;

/**
 * @author chenwy
 */
public class Test {

    public static void main(String[] args) {

       Connection connection =  HBaseInstance.HUGE.getConnection();

        try {

            long timestamp = System.currentTimeMillis() - 80 * 24 * 60 * 60 * 1000L;

            List<String> days = DateUtils.getNaturalMonthDayStringList(timestamp);

            Admin admin = connection.getAdmin();

            for(String day : days){

                TableName tableName = HBaseTable.LIVE_AUTHOR_GIFT.getTableName(day);

                if(admin.tableExists(tableName)){

                    admin.disableTable(tableName);
                    admin.deleteTable(tableName);
                    System.out.println("========" + day);
                }

            }

            List<Pair<String,String>> weeks = DateUtils.getNaturalMonthWeekPairList(timestamp);

            for(Pair<String, String> pair : weeks){
                TableName tableName = HBaseTable.LIVE_AUTHOR_GIFT.getTableName(pair.getLeft(), pair.getRight());
                if(admin.tableExists(tableName)){
                    admin.disableTable(tableName);
                    admin.deleteTable(tableName);
                    System.out.println("========" + pair.getLeft() + "," + pair.getRight());
                }
            }


            String month =  DateUtils.toMonthString(timestamp);

            TableName tableName = HBaseTable.LIVE_AUTHOR_GIFT.getTableName(month);
            if(admin.tableExists(tableName)){

                admin.disableTable(tableName);
                admin.deleteTable(tableName);
                System.out.println("========" + month);
            }




        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
